package com.thinkgem.jeesite.modules.shop.web.order;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.utils.CommonParameter;
import com.thinkgem.jeesite.modules.KDniao.service.logistics.LogisticsCodeComment;
import com.thinkgem.jeesite.modules.dispatch.entity.ShopDispatch;
import com.thinkgem.jeesite.modules.dispatch.service.ShopDispatchService;
import com.thinkgem.jeesite.modules.product.entity.product.GenProduct;
import com.thinkgem.jeesite.modules.product.service.product.GenProductService;
import com.thinkgem.jeesite.modules.shop.constant.order.ShopOrderConstant;
import com.thinkgem.jeesite.modules.shop.entity.info.ShopOrderInfo;
import com.thinkgem.jeesite.modules.shop.entity.order.ShopOrder;
import com.thinkgem.jeesite.modules.shop.service.order.ShopOrderService;

/**
 * 我的订单Controller
 * 
 * @author JiangYongKang
 * @version 2016-12-19
 */
@Controller
@RequestMapping(value = "${frontPath}/shop/order/myOrder")
public class MyOrderController extends LogisticsCodeComment {
    @Autowired
    private ShopOrderService shopOrderService;
    @Autowired
    ShopDispatchService shopDispatchService;
    @Autowired
    GenProductService genProductService;


    // 订单详情
    @RequestMapping(value = "info")
    public String list(@RequestParam("orderId") String orderId, Model model) {
        model.addAttribute("shopOrder", shopOrderService.get(orderId));
        model.addAttribute("path", CommonParameter.uploadPath);
        getinfo(orderId, model, false);
        getlogisticsCodes(model);
        return "modules/shop/order_info/order_info";
    }
	// 订单详情
    @RequestMapping(value = "infoByCode")
    public String infoByCode(@RequestParam("coNum") String coNum, Model model) {
    	model.addAttribute("shopOrder", shopOrderService.findByCoNum(coNum));
        model.addAttribute("path", CommonParameter.uploadPath);
        return "modules/shop/order_info/order_info";
    }
    // 取消订单 status=6   
    // 确认收货 status=5  
    @RequestMapping(value = "edit")
    @ResponseBody
    @Transactional(readOnly = false)
    public String edit(String reason,Short status,
    		          String id, Model model,HttpSession session) {
    	try{
    		  ShopOrder shopOrder=shopOrderService.get(id);
    		  if(status==ShopOrderConstant.ORDER_COMMIT){
    			 //客户确认收货 订单交易完成  同时修改发货单状态
    			  ShopDispatch shopDispatch=shopDispatchService.getByOrderId(id);
    			  shopDispatch.setStatus(ShopOrderConstant.DISPATCHORDER_FINISHED);
    			  shopDispatchService.saves(shopDispatch,session);
    		   }
    		  if(status==ShopOrderConstant.ORDER_CANCEL){
    			  //取消订单  同时增加库存
    			  for (ShopOrderInfo orderInfo : shopOrder.getShopOrderInfoList()) {
    		            String productId = orderInfo.getProductId();
    		            GenProduct product = genProductService.get(productId);
    		            product.setStock(String.valueOf(Integer.parseInt(product.getStock())
    		                    + orderInfo.getCount()));
    		            genProductService.updateStock(product);
    		        }
    			  
    		  }
    		  shopOrder.setStatus(status);
        	  shopOrder.setComment(reason);
        	  shopOrderService.saves(shopOrder,session);
        	  return "success"; 
    	}catch(Exception e){
    		return "error";
    	}
    }
    
    @RequestMapping(value = "forRefund")
    @ResponseBody
    public String forRefund(String reason,String id, Model model,HttpSession session) {
    	try{
    		ShopOrder shopOrder=shopOrderService.get(id);
    		if(shopOrder.getStatus()!=null&&shopOrder.getStatus()==ShopOrderConstant.ORDER_PAYMENT_DONE){
    		    shopOrder.setStatus(ShopOrderConstant.ORDER_REFUND);
                shopOrder.setRefundReason(reason);
                shopOrderService.updateOrderStatusForRefund(shopOrder);
                return "success"; 
    		}
    		return "error";
    	} catch(Exception e){
    		return "error";
    	}
    }
    
    //删除订单  修改订单is_deleted=1
    @RequestMapping(value = "delete")
    @ResponseBody
    public String updateDelete(String id, Model model) {
    	try{
    		  shopOrderService.updateDelete(id);
        	  return "success"; 
    	}catch(Exception e){
    		return "error";
    	}
    }
    

}
